package com.ziyougou.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ziyougou.product.entity.BrandEntity;
import com.ziyougou.product.entity.CategoryEntity;
import com.ziyougou.product.service.BrandService;
import com.ziyougou.product.service.CategoryService;
import com.ziyougou.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ziyougou.product.entity.CategoryBrandRelationEntity;
import com.ziyougou.product.service.CategoryBrandRelationService;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author yuanhaipeng
 * @email yuanhaipeng@gmail.com
 * @date 2024-06-26 14:09:53
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    //根据分类id,查询关联的品牌列表
    @GetMapping("brands/list")
    public R getbrandsByCatId(@RequestParam("catId") Long  catId){
       List<BrandVo>  list= categoryBrandRelationService.getbrandsByCatId(catId);
        return  R.ok().put("data",list);
    }

    /**
     * 根据品牌id，查询该品牌关联的分类目录
     */
    @GetMapping("catelog/list")
    public R  getCatelogListByBrandId(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> catelogList=categoryBrandRelationService.getCatelogListByBrandId(brandId);
        return R.ok().put("data",catelogList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        //1.根据分类id查询分类名称
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        //2.根据品牌iD查询品牌名称
        BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());
        //3.赋值
        categoryBrandRelation.setCatelogName(category.getName());
        categoryBrandRelation.setBrandName(brand.getName());
        //4.保存
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
